<template>
  <div class="article">
    <!-- 导航栏 -->
    <van-nav-bar
      title="文章详情"
      fixed
      left-arrow
      @click-left="$router.back()"
    ></van-nav-bar>

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ info.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="'http://124.223.14.236:8060/' + info.pic"
        />
        <div class="text">
          <p class="name">{{ info.author }}</p>
          <p class="time">4天前</p>
        </div>
      </div>
      <div class="content">
        <div v-html="info.content"></div>
      </div>

      <!--分割线-->
      <van-divider>END</van-divider>
      <h3 class="title">相关推荐</h3>
      <van-grid :column-num="2" center>
        <van-grid-item v-for="item in recommend" :key="item.id">
          <van-image
            :src="'http://124.223.14.236:8060/' + item.pic"
            fit="cover"
            width="109"
            height="90"
          />
          <span class="titles">{{ item.title }}</span>
        </van-grid-item>
      </van-grid>
    </div>
    <van-tabbar>
      <van-tabbar-item
        :icon="info.isCollect ? 'star' : 'star-o'"
        @click="isCollectFn(1)"
      ></van-tabbar-item>
      <van-tabbar-item
        :icon="info.isLike ? 'good-job' : 'good-job-o'"
        @click="isCollectFn(2)"
      ></van-tabbar-item>
      <van-tabbar-item icon="share"></van-tabbar-item>
    </van-tabbar>
  </div>
</template>
<script>
import { acticleID, addCollect } from '@/api/register'
import { mapState } from 'vuex'
export default {
  name: 'Acticle',
  data() {
    return {
      info: '',
      recommend: []
    }
  },
  computed: {
    ...mapState('user', ['token']),
    artID() {
      return this.$route.query.id
    }
  },

  async created() {
    const { data } = await acticleID(this.artID, this.token)
    this.info = data.data.info
    this.recommend = data.data.recommend
  },
  methods: {
    async isCollectFn(num) {
      if (!this.token.length) {
        this.$router.push({
          path: '/login',
          query: {
            back: this.$route.fullPath,
            falg: true
          }
        })
        return this.$toast('请先登录')
      }

      try {
        if (num === 1) {
          await addCollect(num, this.artID, this.info.isCollect ? 'del' : 'add')
          this.info.isCollect = !this.info.isCollect
          this.$toast(this.info.isCollect ? '收藏成功' : '取消收藏')
        } else {
          await addCollect(num, this.artID, this.info.isLike ? 'del' : 'add')
          this.info.isLike = !this.info.isLike
          this.$toast(this.info.isLike ? '点赞成功' : '取消点赞')
        }
      } catch {}
    }
  }
}
</script>
<style lang="less" scoped>
.loading {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.article {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
  .btns {
    width: 50%;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
    align-content: center;
  }
  .titles {
    font-size: 16px;
  }
}

::v-deep {
  .van-nav-bar__content {
    background-color: #3196fa;
  }
  .van-nav-bar__title {
    color: #fff;
  }
  .van-icon.van-icon-arrow-left.van-nav-bar__arrow {
    color: #fff;
  }
  .van-icon-star-o {
    color: rgb(119, 119, 119);
  }
}
</style>
